package com.kailong.filter;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebFilter(filterName = "FilterRegister")
public class FilterRegister implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        req.setCharacterEncoding("utf-8");
        String sno = req.getParameter("userid");
        PrintWriter out = resp.getWriter();
        if (sno.equals("")){
            out.write("<script>");
            out.write("alert('学号不能为空！');");
            out.write("location.href='student/studentRegister.jsp';");
            out.write("<script>");
            return;
        }

        chain.doFilter(req, resp);//通过则使用这条语句,不通过直接return
    }
}
